package com.neuedu.ylbx.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neuedu.ylbx.pojo.DrugInfo;
import com.neuedu.ylbx.service.DrugsService;
import com.neuedu.ylbx.utils.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug")
@Tag(name = "药品管理")
@CrossOrigin
public class DrugsController {    // 药品管理
    @Autowired
    DrugsService drugsService;

    @GetMapping("/drugpage")
    @Operation(summary = "获取药品列表")
    public ResultVo listDrugPage(){
        return ResultVo.ok(drugsService.list());
    }

    @PostMapping("/adddrug")
    @Operation(summary = "添加药品")
    public ResultVo addDrug(DrugInfo drugInfo){
        drugsService.save(drugInfo);
        return ResultVo.ok("添加成功");
    }

    @PutMapping("/updatedrug")
    @Operation(summary = "修改药品")
    public ResultVo updateDrug(DrugInfo drugInfo){
        UpdateWrapper<DrugInfo> updateWrapper = new UpdateWrapper<DrugInfo>();
        updateWrapper.eq("id",drugInfo.getId());
        drugsService.update(drugInfo,updateWrapper);
        return ResultVo.ok("修改成功");
    }

    @DeleteMapping("/deleteDrug")
    @Operation(summary = "删除药品")
    public ResultVo deleteDrug(Integer drugID) throws Exception{
        drugsService.removeById(drugID);
        return ResultVo.ok("删除成功");
    }

    //批量删除
    @DeleteMapping("/deletedrugs")
    @Operation(summary = "批量删除药品")
    public ResultVo deleteDrugs(@RequestBody List<Long> drugIds){
        drugsService.removeByIds(drugIds);  // MyBatis-Plus 批量删除方法
        return ResultVo.ok("批量删除成功");
    }

}
